<?php
/**
 * The employee class.
 * @author Ian Dunn <scholar42@gmail.com>
 * @version 1.0
 * @since 2013-10-30
 */

require_once 'database.php';
require_once 'table.php';

class employee extends table {
	var $first_name;
	var $last_name;
	var $shift;
	var $machine;

	/**
	 * The constructor of the employee class.
	 *
	 * @param i	 The ID of this employee.
	 * @return	 The newly initialized employee object.
	 */
	function __construct($i) {
		parent::__construct ($i,'Employees');
		$this->shift		= -1;
		$this->machine		= -1;
		$this->first_name	= "";
		$this->last_name	= "";
	}

	/**
	 * Initialize this object from data.
	 *
	 * @param shift		  The shift that this employee works.
	 * @param machine	  The machine to place this worker on.
	 * @param first_name  The first name of this employee.
	 * @param last_name	  The last name of this employee.
	 * @return			  None.
	 */
	function init_from_data($shift,$machine,$first_name,$last_name) {
		$this->shift		= $shift;
		$this->machine		= $machine;
		$this->first_name	= $first_name;
		$this->last_name	= $last_name;
	}

	/**
	 * Initialize this employee object from form data.
	 *
	 * @param post	The _POST variable from a form.
	 * @return		Whether or not the init was successful.
	 */
	function init_from_post($post) {
		if (!(isset ($post['shift'])
			 && isset ($post['machine'])
			 && isset ($post['first_name'])
			 && isset ($post['last_name'])))
			return FALSE;
		$this->init_from_data (
			$post['shift'],
			$post['machine'],
			$post['first_name'],
			$post['last_name']
		);
		return TRUE;
	}

	/**
	 * Prints a form to select an employee from the database.
	 *
	 * @param action	The action to perform on submission.
	 */
	static function select ($action) {
		$db = new database ();
		$employees = $db->query ("SELECT id, first_name, last_name FROM Employees");
		echo "<form name=\"employee\" action=\"" . $action . "\" method=\"post\">\n";
		echo "<select name=\"name\">\n";
		while ($row = $employees->fetch_assoc ()) {
			$id = $row['id'];
			$first_name = $row['first_name'];
			$last_name = $row['last_name'];
			echo "<option value='$id'>$first_name $last_name</option>\n";
		}
		echo "</select><br>";
		echo "<input type=\"submit\" value=\"Go\"><br>\n";
		echo "</form><br>\n";
	}

	/**
	 * Displays the form for an employee.
	 *
	 * @param table_name	Only included for compatability.
	 */
	static function display_form ($table_name = "Employees") {
		$table_name = "Employees";

		echo "Add a new employee: <br>\n";
		$type = strtolower ($table_name);
		$type = trim ($type, "s");
		$assoc = get_class_vars (get_called_class ());

		echo "<form name=\"$table_name\" action=\"insert.php?type=$type\" method=\"post\">\n";
		echo "<table> \n";
		foreach ($assoc as $key => $value) {
			if (!strcmp ($key, 'id') || !strcmp ($key, 'tab')) {
				continue;
			}

			echo "<tr>\n";

			$mod_key = ucwords(str_replace("_", " ", $key));
			echo "<td>" . $mod_key . "</td>\n";

			if (!strcmp ($key, 'machine')) {
				echo "<td>\n";
				echo "<select name=\"machine\">\n";
				$db = new database ();
				$machs = $db->query("SELECT id, name FROM Machines");

				// Need to set current selected.
				while ($row = $machs->fetch_assoc()) {
					$id = $row['id']; $name = $row['name'];
					print "\t\t\t\t\t\t<option value='$id'>$name</option>\n";
				}

				$db->close ();
				echo "</select>\n";
				echo "</td>\n";
			} else	{
				echo '<td> <input type="text" name="' . $key . '">' . "\n";
			}
			echo "</tr>\n";
		}

		echo "</table><br>\n";
		echo '<input type="submit" value="Enter"> <br>'."\n";
		echo '</form>'."\n";
	}
}

?>